<?php

namespace App\Http\Controllers\Api;

use App\Http\Resources\BaseResource;
use App\Repositories\Contracts\PolicyUploadInterface;
use Illuminate\Http\Request;
use MakeRep\Controllers\BaseController;

class QnNotifyController extends BaseController
{
    public function __construct(PolicyUploadInterface $interface)
    {
        parent::__construct($interface);
    }

    /**
     * 去哪异步回调
     * @param Request $request
     * @return mixed
     */
    public function notify(Request $request){

        $param = $request->all();
        logger($param);
        if (!empty($param) && !empty($param['ext'])) {

            $methodArray = explode(':',$param['ext']);

            $where = ['ext'=>$methodArray[0]];
            $policy = $this->interface->index($where);
            if ($policy) {

                switch ($methodArray[1]) {

                    case 'add': $this->addMethod($param,$where);break;
                    case 'delete': $this->deleteMethod($param,$where);break;
                }
            }
        }

        return new BaseResource([]);
    }


    /**
     * 添加操作
     * @param $param
     * @param $where
     * @return void
     */
    private function addMethod($param,$where){

        if ($param['result'] == 'Exec successful,0/2 is error!ReplicateCount is 0,confirmCount is 0') {

            $data = ['upload_status'=>2];
        } else {

            $data = ['upload_status'=>3];
        }

        $data['upload_msg'] = $param['result'];

        $this->interface->update($where, $data);
    }

    /**
     * 删除操作
     * @param $param
     * @param $where
     * @return void
     */
    private function deleteMethod($param,$where){

        if ($param['result'] == 'Exec successful,0/1 is error!ReplicateCount is 0,confirmCount is 0') {

            return $this->interface->delete($where);
        }
        $data['upload_msg'] = $param['result'];

        $this->interface->update($where, $data);
    }
}